package com.destin.basic.util;

import cn.hutool.core.util.StrUtil;
import com.destin.basic.exception.ErrorCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@ApiModel("Json响应数据")
public class JsonResponse<T> implements Serializable {
    private static final long serialVersionUID = -7995058338163551623L;
    public static final int SUCCESS_CODE = 200;
    @ApiModelProperty("返回码：200为成功")
    private int code;
    @ApiModelProperty("异常信息")
    private String message;
    @ApiModelProperty("请求ID")
    private String requestId;
    @ApiModelProperty("返回数据")
    private T data;
    @ApiModelProperty("是否正常返回")
    private boolean success;
    @ApiModelProperty("响应参数")
    private Map<String, Object> params;

    public JsonResponse() {
        this.code = 200;
        this.success = true;
    }

    private JsonResponse(T data) {
        this.code = 200;
        this.data = data;
        this.success = true;
    }

    private JsonResponse(int errorCode, T data) {
        this.code = errorCode;
        this.data = data;
    }

    private JsonResponse(int errorCode, String message, T data) {
        this.code = errorCode;
        this.data = data;
        this.message = message;
    }

    private JsonResponse(int status, String message) {
        this.code = status == 0 ? 200 : status;
        this.message = message;
        this.success = status == 200;
    }

    /**
     * 未授权返回结果
     */
    public static <T> JsonResponse<T> forbidden(T data) {
        return new JsonResponse<T>(ErrorCode.FAILED_TYPE_FORBIDDEN, "没有相关权限", data);
    }

    /**
     * 未登录返回结果
     */
    public static <T> JsonResponse<T> unauthorized(T data) {
        return new JsonResponse<T>(ErrorCode.FAILED_TYPE_UNAUTHORIZED, "暂未登录或token已经过期", data);
    }

    public void addParams(String key, Object value) {
        if (!StrUtil.isBlank(key)) {
            if (null == this.params) {
                this.params = new HashMap();
            }

            this.params.put(key, value);
        }
    }

    public static <T> JsonResponse<T> ok() {
        return new JsonResponse();
    }

    public static <T> JsonResponse<T> ok(T data) {
        return new JsonResponse(data);
    }

    public static JsonResponse error(String message) {
        return new JsonResponse(500, message);
    }

    public static JsonResponse error(String message, Object data) {
        return new JsonResponse(500, message, data);
    }

    public static JsonResponse error(int errorCode, String message) {
        return new JsonResponse(errorCode, message);
    }

    public static <T> JsonResponse<T> error(int errorCode, T data) {
        return new JsonResponse(errorCode, data);
    }

    public static JsonResponse badRequest(String message) {
        return new JsonResponse(400, message);
    }
    @Override
    public String toString() {
        return "JsonResponse{code=" + this.code + ", message='" + this.message + '\'' + ", requestId='" + this.requestId + '\'' + ", data=" + this.data + ", success=" + this.success + ", params=" + this.params + '}';
    }

    public int getCode() {
        return this.code;
    }

    public String getMessage() {
        return this.message;
    }

    public String getRequestId() {
        return this.requestId;
    }

    public T getData() {
        return this.data;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public Map<String, Object> getParams() {
        return this.params;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }
    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof JsonResponse)) {
            return false;
        } else {
            JsonResponse<?> other = (JsonResponse)o;
            if (!other.canEqual(this)) {
                return false;
            } else if (this.getCode() != other.getCode()) {
                return false;
            } else {
                label65: {
                    Object this$message = this.getMessage();
                    Object other$message = other.getMessage();
                    if (this$message == null) {
                        if (other$message == null) {
                            break label65;
                        }
                    } else if (this$message.equals(other$message)) {
                        break label65;
                    }

                    return false;
                }

                Object this$requestId = this.getRequestId();
                Object other$requestId = other.getRequestId();
                if (this$requestId == null) {
                    if (other$requestId != null) {
                        return false;
                    }
                } else if (!this$requestId.equals(other$requestId)) {
                    return false;
                }

                Object this$data = this.getData();
                Object other$data = other.getData();
                if (this$data == null) {
                    if (other$data != null) {
                        return false;
                    }
                } else if (!this$data.equals(other$data)) {
                    return false;
                }

                if (this.isSuccess() != other.isSuccess()) {
                    return false;
                } else {
                    Object this$params = this.getParams();
                    Object other$params = other.getParams();
                    if (this$params == null) {
                        if (other$params != null) {
                            return false;
                        }
                    } else if (!this$params.equals(other$params)) {
                        return false;
                    }

                    return true;
                }
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof JsonResponse;
    }

    @Override
    public int hashCode() {
        int result = 1;
        result = result * 59 + this.getCode();
        Object message = this.getMessage();
        result = result * 59 + (message == null ? 43 : message.hashCode());
        Object requestId = this.getRequestId();
        result = result * 59 + (requestId == null ? 43 : requestId.hashCode());
        Object data = this.getData();
        result = result * 59 + (data == null ? 43 : data.hashCode());
        result = result * 59 + (this.isSuccess() ? 79 : 97);
        Object params = this.getParams();
        result = result * 59 + (params == null ? 43 : params.hashCode());
        return result;
    }
}
